package com.major.tree.threadbinary;

public class ThreadNode {
    private int no;
    private String name;
    private ThreadNode leftNode;
    private ThreadNode rightNode;
    // 0表示指向左子树，1表示指向前驱结点
    private int leftType;
    private int rightType;

    @Override
    public String toString() {
        return "ThreadNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public ThreadNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ThreadNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(ThreadNode leftNode) {
        this.leftNode = leftNode;
    }

    public ThreadNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(ThreadNode rightNode) {
        this.rightNode = rightNode;
    }

    // 中序遍历
    public void inoOrder() {
        if (leftNode != null) {
            leftNode.inoOrder();
        }
        System.out.println(this.toString());
        if (rightNode != null) {
            rightNode.inoOrder();
        }
    }

    // 查询
    // 线索化后判断节点类型
    public ThreadNode getNodeByNo(int no) {
        ThreadNode resNode = null;

        if (leftNode != null) {
            resNode = leftNode.getNodeByNo(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no) {
            return this;       // 只返回了这一个值
        }
        if (this.rightNode != null) {
            resNode = this.rightNode.getNodeByNo(no);
        }
        return resNode;
    }

    // 删除
    public void deleteByNo(int no) {
        if (leftNode != null && leftNode.no == no) {
            leftNode = null;
            return;
        }
        if (leftNode != null) {
            leftNode.deleteByNo(no);
        }

        if (rightNode != null && rightNode.no == no) {
            rightNode = null;
            return;
        }
        if (rightNode != null){
            rightNode.deleteByNo(no);
        }

    }
}

